<template>
  <div>
    <!-- 顶部 -->
    <nav-top class="navtop"
      >排行榜
      <template v-slot:gender>
        <div class="middle" v-show="!$route.meta.gender">
          <button
            :class="{ active: $store.state.ismale == 'male' }"
            @click="active('male')"
          >
            男生
          </button>
          <button
            :class="{ active: $store.state.ismale == 'female' }"
            @click="active('female')"
          >
            女生
          </button>
        </div>
      </template>
    </nav-top>
    <!-- 内容 -->
    <div class="item" v-for="(item, index) in rank[gender]" :key="index">
      <div class="imgs">
        <img
          src="../assets/images/rank01.jpg"
          alt=""
          @click="toRouter('rank/list', { tit: item.shortTitle, id: item._id })"
        />
        <!-- <img :src="'../assets/images/rank0' + (index + 1) + '.jpg'" alt="" /> -->
        <h2>{{ item.shortTitle }}</h2>
        <span class="iconfont icon-arrow-right-"></span>
      </div>
      <div class="list">
        <ol v-if="item.be != 'be'">
          <li
            v-for="(k, i) in item.arr"
            :key="i"
            @click="toRouter('details', { id: k._id })"
          >
            <i>{{ i + 1 }}.</i>
            <span>{{ k.title }}</span
            ><span class="iconfont icon-arrow-right-"></span>
          </li>
        </ol>
        <div v-else>
          <van-empty description="该榜单小说暂无" image-size="3rem" />
        </div>
      </div>
    </div>
    <van-overlay :show="overlay">
      <div class="wrapper">
        <van-loading type="spinner" color="#1989fa" />
      </div>
    </van-overlay>
  </div>
</template>


<script>
import { getCategory, getrank } from "../api/rank/all.js";
export default {
  data() {
    return {
      rank: {
        male: null,
        female: null,
      },
      overlay: false, // 遮罩层 加载
    };
  },
  methods: {
    active(flag) {
      this.$store.commit("setIsmale", flag);
    },
    toRouter(path, params) {
      this.$router.push({
        path: `/${path}`,
        query: { ...params },
      });
    },
    getData() {
      this.overlay = true;
      getCategory()
        .then((data) => {
          if (data.ok) {
            this.rank.male = data.male.splice(0, 6);
            this.rank.female = data.female.splice(0, 6);
          }
          return this.rank;
        })
        .then((rank) => {
          rank.male.forEach((element, index) => {
            getrank(element._id).then((data) => {
              if (data.ranking.books.length == 0) {
                this.$set(this.rank["male"][index], "be", "be");
              }
              this.$set(
                this.rank["male"][index],
                "arr",
                data.ranking.books.splice(0, 5)
              );
            });
          });
          rank.female.forEach((element, index) => {
            getrank(element._id).then((data) => {
              if (data.ranking.books.length == 0) {
                this.$set(this.rank["female"][index], "be", "be");
              }
              this.$set(
                this.rank["female"][index],
                "arr",
                data.ranking.books.splice(0, 5)
              );
            });
          });
        })
        .then(() => {
          this.overlay = false;
        });
    },
  },
  created() {
    this.getData();
  },
  filters: {
    imgUrl(url) {
      return "http://statics.zhuishushenqi.com" + url;
    },
  },
  computed: {
    gender() {
      return this.$store.state.ismale;
    },
  },
};
</script>

<style lang="less" scoped>
.navtop {
  background-color: white;
}
.item {
  margin-bottom: 0.3125rem;
  display: flex;
  font-size: 0;
  .imgs {
    position: relative;
    width: 3.125rem;
    img {
      width: 100%;
      height: auto;
    }
    h2 {
      width: 100%;
      position: absolute;
      top: 20%;
      font-size: 0.78125rem;
      color: white;
      text-align: center;
      font-family: "LiSu";
      line-height: 0.6rem;
    }
    span {
      position: absolute;
      width: 0.625rem;
      height: 0.625rem;
      text-align: center;
      line-height: 0.625rem;
      background-color: white;
      top: 68%;
      left: 50%;
      transform: translateX(-50%);
      border-radius: 50%;
    }
  }
  .list {
    background-color: white;
    align-items: stretch;
    flex: 1;
    width: 100px;
    div {
      height: 100%;
      font-size: 0.3125rem;
      .van-empty {
        padding: 0;
      }
    }
    ol {
      height: 100%;
      display: flex;
      flex-direction: column;
      padding-left: 0.6875rem;
      li {
        flex: 1;
        font-size: 0.390625rem;
        font-weight: bold;
        border-left: 1px solid #f0f1f2;
        border-bottom: 1px solid #f0f1f2;
        text-indent: -0.4625rem;
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding-right: 0.625rem;
        &:active {
          background-color: #f2f2f2;
        }
        span {
          &:nth-child(2) {
            text-indent: 0rem;
            width: 90%;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            i {
              margin-right: 0.15625rem;
              font-style: normal;
            }
          }
          &:nth-child(3) {
            font-weight: normal;
            color: #bbb;
          }
        }
      }
    }
  }
}
</style>